﻿Partial Public Class ProductionOrderExplorer

    Public aLotDataRow As DataRow
    Public aProductDataRow As DataRow
    Public aCurrentNewRow As DataRow
    Public Overridable Sub ValidateDetailRow_Picking(ByVal sender As Object, ByVal e As Views.Base.ValidateRowEventArgs)
        Dim row As DataRow = DirectCast(e.Row, DataRowView).Row
        If row IsNot Nothing Then
            Select Case row.Table.TableName
                Case "LotPicking_LotTransaction"
                    Using lotTran As SystemBoxSDK.PL.MESSDK.LotTransaction_PL = New SystemBoxSDK.PL.MESSDK.LotTransaction_PL()
                        lotTran.ValidateRow(row)
                    End Using
                Case Else
            End Select
        End If
    End Sub


    Public Overridable Sub LockColumns(ByVal view As GridView)
        For Each col As GridColumn In view.Columns
            col.OptionsColumn.AllowEdit = False
        Next
    End Sub

    Public Overridable Sub UnLockColumns(ByVal view As GridView)
        For Each col As GridColumn In view.Columns
            col.OptionsColumn.AllowEdit = True
        Next
    End Sub

    Public Overridable Sub DetailRowClick_Picking(ByVal sender As Object, ByVal e As RowClickEventArgs)
        Dim aView As GridView = DirectCast(sender, GridView)
        Dim aRow As DataRow = aView.GetFocusedDataRow()

        If aView.FocusedRowHandle = GridControl.AutoFilterRowHandle Then
            UnLockColumns(aView)
        ElseIf aRow IsNot Nothing Then
            '
            If Not IsDBNull(aRow("StatusID_FK")) AndAlso DirectCast(aRow("StatusID_FK"), Guid) = IdRepository.APP_TRANSTATUS_OPEN Then
                PrepareColumn_Picking(aView)
            Else
                LockColumns(aView)
            End If
            '
        End If
    End Sub

    Public Overridable Sub PrepareColumn_Picking(ByVal view As GridView)
        Dim aRow As DataRow = view.GetFocusedDataRow()

        view.Columns("InvBatchID_FK").OptionsColumn.AllowEdit = True
        view.Columns("TranDate").OptionsColumn.AllowEdit = True
        view.Columns("LotID_FK").OptionsColumn.AllowEdit = True
        view.Columns("FROMWhBinID_FK").OptionsColumn.AllowEdit = True
        view.Columns("TOWhBinID_FK").OptionsColumn.AllowEdit = True
        view.Columns("Qty").OptionsColumn.AllowEdit = True
        view.Columns("RevisedQty").OptionsColumn.AllowEdit = True
        view.Columns("RevisedTranUnitId_FK").OptionsColumn.AllowEdit = True

    End Sub

    Public Overridable Sub CustomRowCellEditForEditing(ByVal sender As Object, ByVal e As CustomRowCellEditEventArgs)
        Dim aView As GridView = DirectCast(sender, GridView)
        Dim aRTEdit As RepositoryItemButtonRichTextEditExt
        Dim aLov As New RepositoryItemGridLookUpEdit()

        Dim aRow As DataRow = aView.GetFocusedDataRow()

        'If Not IsNothing(aView.Columns("LotDocument")) Then
        If e.Column.FieldName = "LotDocument" Then
            aRTEdit = New RepositoryItemButtonRichTextEditExt
            If e.CellValue IsNot Nothing Then
                aRTEdit.Text = e.CellValue.ToString
            End If
            aView.GridControl.RepositoryItems.Add(aRTEdit)
            e.RepositoryItem = aRTEdit
        End If
        'End If

    End Sub

    Public Overridable Sub InitNewDetailRow_Picking(ByVal sender As Object, ByVal e As InitNewRowEventArgs)
        Dim view As GridView = CType(sender, GridView)
        Dim row As DataRow = view.GetFocusedDataRow()
        Dim parentview As GridView = CType(view.ParentView, GridView)
        Dim parentrow As DataRow = parentview.GetFocusedDataRow

        If row IsNot Nothing Then
            Select Case row.Table.TableName
                Case "LotPicking_LotTransaction"
                    Using lotTran As SystemBoxSDK.PL.MESSDK.LotTransaction_PL = New SystemBoxSDK.PL.MESSDK.LotTransaction_PL()
                        lotTran.NewRecord(row)
                        AssignDefaultValue_Picking(row)

                        'gros hack car les lien entre les tables sont différent avec Express...
                        'ParentRow contient des donnes de inventorybatch header.
                        If parentrow IsNot Nothing Then
                            If IsDBNull(row("InvBatchID_FK")) AndAlso Not IsDBNull(parentrow("InvBatchID")) Then
                                row("InvBatchID_FK") = parentrow("InvBatchID")
                            End If

                            If IsDBNull(row("TOWhBinID_FK")) AndAlso Not IsDBNull(parentrow("ToWhBinID_FK")) Then
                                row("TOWhBinID_FK") = parentrow("ToWhBinID_FK")
                            End If
                        End If
                    End Using
                Case Else
            End Select
        End If
    End Sub

    Public Overridable Sub InitRow_Picking(row As DataRow)
        row("Qty") = DBNull.Value
        row("PrdId_FK") = DBNull.Value
        row("LotTypeId_FK") = DBNull.Value
        row("ParentLotId_FK") = DBNull.Value
        row("FROMLotStatusId_FK") = DBNull.Value
        row("TOLotStatusId_FK") = DBNull.Value
        row("FROMWhBinID_FK") = DBNull.Value
        row("TranUnitId_FK") = DBNull.Value

        row("RevisedQty") = DBNull.Value
        row("RevisedTranUnitId_FK") = DBNull.Value

        '
        row("Crtd_DT") = Date.Now
        row("Crtd_UId") = AppManager.UserName
        row("LUpd_DT") = Date.Now
        row("LUpd_UId") = AppManager.UserName
    End Sub

    Public Overridable Sub DetailCellValueChanged_Picking(ByVal sender As Object, ByVal e As DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs)
        Dim view As GridView = DirectCast(sender, GridView)
        Dim row As DataRow = view.GetFocusedDataRow()
        Dim parentview As GridView = CType(view.ParentView, GridView)

        Dim parentrow As DataRow = parentview.GetFocusedDataRow

        If row IsNot Nothing Then
            If row.Table.TableName = "LotPicking_LotTransaction" Then
                If e.Column.FieldName = "LotID_FK" Then
                    InitRow_Picking(row)
                End If
            End If
        End If

        If Not IsDBNull(row("LotID_FK")) Then
            aLotDataRow = SystemBoxSDK.PL.MESSDK.Lot_PL.GetLotByID(DirectCast(row("LotID_FK"), Guid))
            If aLotDataRow IsNot Nothing Then
                row("PrdId_FK") = aLotDataRow("PrdId_FK")
                row("LotTypeId_FK") = aLotDataRow("LotTypeId_FK")
                row("ParentLotId_FK") = parentrow("LotId_FK")
                row("FROMLotStatusId_FK") = aLotDataRow("StatusID_FK")
                row("LotCD") = aLotDataRow("LotDescr_Prm")
            End If
        End If
        '
        If Not IsDBNull(row("PrdId_FK")) Then
            aProductDataRow = SystemBoxSDK.PL.CoreSDK.Product_PL.GetProductByID(DirectCast(row("PrdId_FK"), Guid))
            If aProductDataRow IsNot Nothing Then
                row("TranUnitId_FK") = aProductDataRow("StorageUnitId_FK")
            End If
        End If
    End Sub

    Public Overridable Sub DetailCustomRowCellEditForEditing_Picking(ByVal sender As Object, ByVal e As CustomRowCellEditEventArgs)
        Dim aDetailview As GridView = DirectCast(sender, GridView)
        Dim aLotTranRow As DataRow = aDetailview.GetFocusedDataRow()
        Dim aEdit As RepositoryItemGridLookUpEdit
        Dim aLotId As Guid
        Dim sqlWhere As String


        'pour l'instant des Lot avec Des LotQtyAvail... AL
        If e.Column.FieldName = "LotID_FK" Then
            sqlWhere = "a.LotQtyAvail > 0.0"
            aEdit = LOVBuilder("LOV_Lot", sqlWhere, Nothing, Nothing)

            aEdit.Name = "LOV_Lot"
            aDetailview.GridControl.RepositoryItems.Add(aEdit)
            e.RepositoryItem = aEdit
        End If
        '
        If aLotTranRow IsNot Nothing Then
            Select Case aLotTranRow.Table.TableName
                Case "LotPicking_LotTransaction"
                    If e.Column.FieldName = "FROMWhBinID_FK" Then
                        aLotId = CType(If(IsDBNull(aLotTranRow("LotID_FK")), Guid.Empty, aLotTranRow("LotID_FK")), Guid)

                        sqlWhere = String.Format("LotStock.LotID_FK = '{0}' AND Site.SiteId <> '{1}' AND LotStock.QtyAvail > 0.0", aLotId.ToString, IdRepository.APP_SITE_ADJUST.ToString)
                        aEdit = LOVBuilder("LOV_SiteSectionWithStock", sqlWhere, Nothing, Nothing)

                        aEdit.Name = "LOV_SiteSectionWithStock"
                        aDetailview.GridControl.RepositoryItems.Add(aEdit)
                        e.RepositoryItem = aEdit
                    End If
                Case Else
            End Select
        End If
    End Sub

    Public Overridable Sub AssignDefaultValue_Picking(ByVal row As DataRow)
        row("TranTypeID_FK") = IdRepository.APP_TRAN_ISSUEWIP
        row("StatusID_FK") = IdRepository.APP_TRANSTATUS_OPEN

        If IsDBNull(row("TranDate")) Then
            row("TranDate") = Date.Today
        End If
    End Sub

    Public Overridable Sub DeleteRow_Picking(row As DataRow, e As CancelEventArgs)
        If row.Table.TableName = "LotPicking_LotTransaction" Then
            If Not IsDBNull(row("StatusID_FK")) AndAlso DirectCast(row("StatusID_FK"), Guid) <> IdRepository.APP_TRANSTATUS_OPEN Then
                MessageWindow.ShowMessage("Only Open Transaction Can Be Deleted", MsgBoxStyle.Information)
                e.Cancel = True
            End If
        End If
    End Sub



    Public Overridable Sub ProcessCurrentDetailRecord(ByVal sender As Object, ByVal e As Views.Base.ValidateRowEventArgs, ByVal index As Integer)
        Dim aRow As DataRow = DirectCast(e.Row, DataRowView).Row
        If aRow IsNot Nothing Then
            If aRow IsNot Nothing Then
                Select Case aRow.Table.TableName
                    Case "LotBOM_R"
                        aRow("ConvertedQty") = aRow("Target")
                        aRow("ConvertedUnitID_FK") = aRow("UnitID_FK")

                        'Hack: Le temps que la BD soit fini d'être convertit
                        If Not IsDBNull(aRow("ScalingId_FK")) Then
                            Select Case CType(aRow("ScalingId_FK"), Guid)
                                Case IdRepository.APP_BOM_SCALING_SCALED
                                    aRow("Scaling") = 0
                                Case IdRepository.APP_BOM_SCALING_FIXED
                                    aRow("Scaling") = 2
                                Case IdRepository.APP_BOM_SCALING_EXCIPIENT
                                    aRow("Scaling") = 3
                                Case IdRepository.APP_BOM_SCALING_BY_LOT
                                    aRow("Scaling") = 1
                                Case Else
                            End Select
                        End If
                End Select
            End If
        End If
    End Sub

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Public Overridable Sub InitNewMasterRow(ByVal sender As Object, ByVal e As InitNewRowEventArgs)
        Dim view As GridView = CType(sender, GridView)
        Dim row As DataRow = view.GetFocusedDataRow()
        Using aPlLot As MESSDK.Lot_PL = New PL.MESSDK.Lot_PL()
            aPlLot.NewRecord(row)
        End Using
    End Sub

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Public Overridable Sub ShownMasterEditor(ByVal sender As Object, e As System.EventArgs)
        Dim aView As GridView = CType(sender, GridView)
        Dim aRow As DataRow = aView.GetFocusedDataRow()
        Dim edit As GridLookUpEdit


        If TypeOf aView.ActiveEditor Is GridLookUpEdit Then
            edit = CType(aView.ActiveEditor, GridLookUpEdit)
            edit.Properties.View.ActiveFilterEnabled = False

            Dim SiteId As Guid = Guid.Empty
            If aRow IsNot Nothing Then
                SiteId = If(IsDBNull(aRow("WhouseID_FK")), Guid.Empty, CType(aRow("WhouseID_FK"), Guid))
            End If
            If aView.FocusedColumn.FieldName = "PrdID_FK" And Not aView.FocusedRowHandle.Equals(GridControl.AutoFilterRowHandle) Then
                'SiteId = If(IsDBNull(aRow("WhouseID_FK")), Guid.Empty, CType(aRow("WhouseID_FK"), Guid))
                edit.Properties.View.ActiveFilterString = String.Format("Inactive = False and SiteId_FK = '{0}'", SiteId)
                edit.Properties.View.OptionsView.ShowFilterPanelMode = Views.Base.ShowFilterPanelMode.Never

            ElseIf aView.FocusedColumn.FieldName = "TOWhBinID_FK" And Not aView.FocusedRowHandle.Equals(GridControl.AutoFilterRowHandle) Then
                'SiteId = If(IsDBNull(aRow("WhouseID_FK")), Guid.Empty, CType(aRow("WhouseID_FK"), Guid))
                edit.Properties.View.ActiveFilterString = String.Format("SiteId = '{0}'", SiteId)
                edit.Properties.View.OptionsView.ShowFilterPanelMode = Views.Base.ShowFilterPanelMode.Never
            End If

        End If
    End Sub

    Public Overridable Sub ShownDetailEditor(ByVal sender As Object, e As System.EventArgs)
        Dim aView As GridView = CType(sender, GridView)
        Dim aRow As DataRow = aView.GetFocusedDataRow()
        Dim edit As GridLookUpEdit

        Dim parentview As GridView = CType(aView.ParentView, GridView)
        Dim parentrow As DataRow = parentview.GetFocusedDataRow


        Dim SiteId As Guid = Guid.Empty

        If TypeOf aView.ActiveEditor Is GridLookUpEdit Then
            edit = CType(aView.ActiveEditor, GridLookUpEdit)
            edit.Properties.View.ActiveFilterEnabled = False

            If parentrow IsNot Nothing Then
                SiteId = If(IsDBNull(parentrow("WhouseID_FK")), Guid.Empty, CType(parentrow("WhouseID_FK"), Guid))
            End If

            If aView.FocusedColumn.FieldName = "CPrdID_FK" And Not aView.FocusedRowHandle.Equals(GridControl.AutoFilterRowHandle) Then
                edit.Properties.View.ActiveFilterString = String.Format("WhouseID = '{0}'", SiteId)
                edit.Properties.View.OptionsView.ShowFilterPanelMode = Views.Base.ShowFilterPanelMode.Never
            End If
        End If
    End Sub

    Public Overridable Sub ProcessCurrentMasterRecord(ByVal sender As Object, ByVal e As DevExpress.XtraGrid.Views.Base.ValidateRowEventArgs)
        Dim productRow As DataRow
        Dim row As DataRow = CType(e.Row, DataRowView).Row

        If row IsNot Nothing AndAlso Not IsDBNull(row("PrdID_FK")) Then
            productRow = CoreSDK.Product_PL.GetProductByID(CType(row("PrdID_FK"), Guid))
            If productRow IsNot Nothing Then
                row("UnitID_FK") = productRow("StorageUnitID_FK")
                row("ProductionUnitId_FK") = productRow("ProductionUnitID_FK")
            End If
        End If

    End Sub

    Public Overridable Sub InitLotStock(ByVal row As DataRow)
        row("QtyAvail") = 0
        row("QtyOnHand") = 0
        row("QtyReserved") = 0
        row("QtyRequested") = 0
        row("QtyWIP") = 0
        row("QtyQI") = 0
        row("QtyObsolete") = 0
    End Sub

    Public Overridable Sub InitLotBOM(ByVal row As DataRow)
        row("Revision") = DBNull.Value
        row("Target") = DBNull.Value
        row("Minimum") = DBNull.Value
        row("Maximum") = DBNull.Value
        row("UnitID_FK") = DBNull.Value
        row("ConvertedQty") = DBNull.Value
        row("ConvertedUnitID_FK") = DBNull.Value
        row("SOP") = DBNull.Value
        row("Method") = DBNull.Value
        row("Scaling") = DBNull.Value
        row("ScalingId_FK") = DBNull.Value
        row("PickingDisplay") = DBNull.Value
        row("Alternate") = DBNull.Value
        row("Crtd_UId") = AppManager.UserName
        row("LUpd_UId") = AppManager.UserName
    End Sub

    Public Overridable Sub InitNewDetailRow(ByVal sender As Object, ByVal e As InitNewRowEventArgs)
        Dim view As GridView = CType(sender, GridView)
        Dim row As DataRow = view.GetFocusedDataRow()
        Dim parentview As GridView = CType(view.ParentView, GridView)
        Dim parentrow As DataRow = parentview.GetFocusedDataRow

        If row IsNot Nothing Then
            Select Case row.Table.TableName
                Case "LotStock_R"
                    InitLotStock(row)
                    Dim iLotStockProcess As SystemBoxSDK.PL.IPLProcess = TryCast(PLBase.CreateInstanceOf(PLTypeOf.LotStock_PL), IPLProcess)
                    iLotStockProcess.NewRecord(row)
                    row("LotID_FK") = parentrow("LotID")
                Case "LotBOM_R"
                    InitLotBOM(row)
                    Dim iLotBOMProcess As SystemBoxSDK.PL.IPLProcess = TryCast(PLBase.CreateInstanceOf(PLTypeOf.LotBOM_PL), IPLProcess)
                    iLotBOMProcess.NewRecord(row)
                    row("LotID_FK") = parentrow("LotID")
                    row("WhouseID_FK") = parentrow("WhouseID_FK")
                Case Else
            End Select
        End If
    End Sub

    Public Overridable Sub DetailCellValueChanged(ByVal sender As Object, ByVal e As DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs)
        Dim view As GridView = DirectCast(sender, GridView)
        Dim row As DataRow = view.GetFocusedDataRow()

        If row IsNot Nothing Then
            If row.Table.TableName = "LotBOM_R" Then
                If e.Column.FieldName = "WhouseID_FK" Then
                    InitLotBOM(row)
                End If
                If e.Column.FieldName = "CPrdID_FK" Then
                    InitLotBOM(row)
                End If
            End If
        End If

    End Sub

    Public Overridable Sub ValidateDetailRow(ByVal sender As Object, ByVal e As Views.Base.ValidateRowEventArgs)
        Dim row As DataRow = DirectCast(e.Row, DataRowView).Row
        If row IsNot Nothing Then
            Select Case row.Table.TableName
                Case "LotStock_R"
                    Using lotStock As SystemBoxSDK.PL.PLBase = PLBase.CreateInstanceOf(PLTypeOf.LotStock_PL)
                        lotStock.ValidateRow(row)
                    End Using
                Case "LotBOM_R"
                    Using lotBOM As SystemBoxSDK.PL.PLBase = PLBase.CreateInstanceOf(PLTypeOf.LotBOM_PL)
                        lotBOM.ValidateRow(row)
                    End Using
            End Select
        End If
    End Sub

    Public Overridable Sub ActionReserveALL(ByVal sender As System.Object, ByVal e As EventArgs)
        Dim aDataRow As DataRow = CType(CType(sender, DXMenuItem).Tag, DataRow)
        Dim alot As PLBase = PLBase.CreateInstanceOf(PLTypeOf.Lot_PL)
        Dim iLot As PL.MESSDK.ILot_PL

        Try
            iLot = TryCast(alot, PL.MESSDK.ILot_PL)
            If iLot IsNot Nothing Then
                iLot.ReserveDetails(aDataRow)
            End If
            MessageWindow.ShowMessage("All Lines have been Reserved", MsgBoxStyle.Information)
        Catch ex As Exception
            MessageWindow.ShowMessage(ex, MsgBoxStyle.Information)
        End Try
    End Sub

    Public Overridable Sub ActionConfirmALL(ByVal sender As System.Object, ByVal e As EventArgs)
        Dim aDataRow As DataRow = CType(CType(sender, DXMenuItem).Tag, DataRow)
        Dim alot As PLBase = PLBase.CreateInstanceOf(PLTypeOf.Lot_PL)
        Dim iLot As PL.MESSDK.ILot_PL
        Try
            iLot = TryCast(alot, PL.MESSDK.ILot_PL)
            If iLot IsNot Nothing Then
                iLot.ConfirmDetails(aDataRow)
            End If
            MessageWindow.ShowMessage("All Lines have been Confirmed", MsgBoxStyle.Information)
        Catch ex As Exception
            MessageWindow.ShowMessage(ex, MsgBoxStyle.Information)
        End Try
    End Sub

    Public Overridable Sub ActionReleaseALL(ByVal sender As System.Object, ByVal e As EventArgs)
        Dim aDataRow As DataRow = CType(CType(sender, DXMenuItem).Tag, DataRow)
        Dim alot As PLBase = PLBase.CreateInstanceOf(PLTypeOf.Lot_PL)
        Dim iLot As PL.MESSDK.ILot_PL
        Try
            iLot = TryCast(alot, PL.MESSDK.ILot_PL)
            If iLot IsNot Nothing Then
                iLot.ReleaseDetails(aDataRow)
            End If
            MessageWindow.ShowMessage("All Lines have been Released", MsgBoxStyle.Information)
        Catch ex As Exception
            MessageWindow.ShowMessage(ex, MsgBoxStyle.Information)
        End Try
    End Sub

    Public Overridable Sub ActionRevertLine(ByVal sender As System.Object, ByVal e As EventArgs)
        'On est sur un DetailView donc on utilise le GridSelectedRows
        Dim aGridSelectedRow As GridSelectedRows = CType(CType(sender, DXMenuItem).Tag, GridSelectedRows)
        Dim alotTran As PLBase = PLBase.CreateInstanceOf(PLTypeOf.LotTransaction_PL)
        Dim iLotTran As PL.MESSDK.ILotTransaction_PL
        ' On pourrait ajouter un message comme quoi il est déjà Released
        Try
            iLotTran = TryCast(alotTran, PL.MESSDK.ILotTransaction_PL)
            If iLotTran IsNot Nothing Then
                If iLotTran.RevertLine(aGridSelectedRow.CurrentSelectedDataRow) Then
                    MessageWindow.ShowMessage("Line is now at Open Status", MsgBoxStyle.Information)
                End If
            End If
        Catch ex As Exception
            MessageWindow.ShowMessage(ex, MsgBoxStyle.Information)
        End Try
    End Sub

    Public Overridable Sub ActionReserveLine(ByVal sender As System.Object, ByVal e As EventArgs)
        'On est sur un DetailView donc on utilise le GridSelectedRows
        Dim aGridSelectedRow As GridSelectedRows = CType(CType(sender, DXMenuItem).Tag, GridSelectedRows)
        Dim alotTran As PLBase = PLBase.CreateInstanceOf(PLTypeOf.LotTransaction_PL)
        Dim iLotTran As PL.MESSDK.ILotTransaction_PL

        ' On pourrait ajouter un message comme quoi il est déjà Reserved
        Try
            iLotTran = TryCast(alotTran, PL.MESSDK.ILotTransaction_PL)
            If iLotTran IsNot Nothing Then
                If iLotTran.ReserveLine(aGridSelectedRow.CurrentSelectedDataRow) Then
                    MessageWindow.ShowMessage("Line Reserved", MsgBoxStyle.Information)
                End If
            End If
        Catch ex As Exception
            MessageWindow.ShowMessage(ex, MsgBoxStyle.Information)
        End Try
    End Sub

    Public Overridable Sub ActionConfirmLine(ByVal sender As System.Object, ByVal e As EventArgs)
        'On est sur un DetailView donc on utilise le GridSelectedRows
        Dim aGridSelectedRow As GridSelectedRows = CType(CType(sender, DXMenuItem).Tag, GridSelectedRows)
        Dim alotTran As PLBase = PLBase.CreateInstanceOf(PLTypeOf.LotTransaction_PL)
        Dim iLotTran As PL.MESSDK.ILotTransaction_PL

        ' On pourrait ajouter un message comme quoi il est déjà Confirmed
        Try
            iLotTran = TryCast(alotTran, PL.MESSDK.ILotTransaction_PL)
            If iLotTran IsNot Nothing Then
                If iLotTran.ConfirmLine(aGridSelectedRow.CurrentSelectedDataRow) Then
                    MessageWindow.ShowMessage("Line Confirmed", MsgBoxStyle.Information)
                End If
            End If
        Catch ex As Exception
            MessageWindow.ShowMessage(ex, MsgBoxStyle.Information)
        End Try

    End Sub

    Public Overridable Sub ActionReleaseLine(ByVal sender As System.Object, ByVal e As EventArgs)
        'On est sur un DetailView donc on utilise le GridSelectedRows
        Dim aGridSelectedRow As GridSelectedRows = CType(CType(sender, DXMenuItem).Tag, GridSelectedRows)
        Dim alotTran As PLBase = PLBase.CreateInstanceOf(PLTypeOf.LotTransaction_PL)
        Dim iLotTran As PL.MESSDK.ILotTransaction_PL
        ' On pourrait ajouter un message comme quoi il est déjà Released
        Try
            iLotTran = TryCast(alotTran, PL.MESSDK.ILotTransaction_PL)
            If iLotTran IsNot Nothing Then
                If iLotTran.ReleaseLine(aGridSelectedRow.CurrentSelectedDataRow) Then
                    MessageWindow.ShowMessage("Line Released", MsgBoxStyle.Information)
                End If
            End If
        Catch ex As Exception
            MessageWindow.ShowMessage(ex, MsgBoxStyle.Information)
        End Try
    End Sub

    Public Overridable Sub ActionReserveComponents(ByVal sender As System.Object, ByVal e As EventArgs)
        Dim aDataRow As DataRow = CType(CType(sender, DXMenuItem).Tag, DataRow)
        Dim alotTran As PLBase = PLBase.CreateInstanceOf(PLTypeOf.LotTransaction_PL)
        Dim iLotTran As PL.MESSDK.ILotTransaction_PL
        Try
            iLotTran = TryCast(alotTran, PL.MESSDK.ILotTransaction_PL)
            If iLotTran IsNot Nothing Then
                'iLotTran.xxxxx(aDataRow)
                MsgBox("In Developpement")
            End If
            'MessageWindow.ShowMessage("All Components have been Reserved", MsgBoxStyle.Information)
        Catch ex As Exception
            MessageWindow.ShowMessage(ex, MsgBoxStyle.Information)
        End Try
    End Sub

    Public Overridable Sub ActionConfirmComponents(ByVal sender As System.Object, ByVal e As EventArgs)
        Dim aDataRow As DataRow = CType(CType(sender, DXMenuItem).Tag, DataRow)
        Dim alotTran As PLBase = PLBase.CreateInstanceOf(PLTypeOf.LotTransaction_PL)
        Dim iLotTran As PL.MESSDK.ILotTransaction_PL
        Try
            iLotTran = TryCast(alotTran, PL.MESSDK.ILotTransaction_PL)
            If iLotTran IsNot Nothing Then
                'iLotTran.xxxx(aDataRow)
                MsgBox("In Developpement")
            End If
            'MessageWindow.ShowMessage("All Components have been Confirmed", MsgBoxStyle.Information)
        Catch ex As Exception
            MessageWindow.ShowMessage(ex, MsgBoxStyle.Information)
        End Try
    End Sub

    Public Overridable Sub ActionReleaseComponents(ByVal sender As System.Object, ByVal e As EventArgs)
        Dim aDataRow As DataRow = CType(CType(sender, DXMenuItem).Tag, DataRow)
        Dim alotTran As PLBase = PLBase.CreateInstanceOf(PLTypeOf.LotTransaction_PL)
        Dim iLotTran As PL.MESSDK.ILotTransaction_PL
        Try
            iLotTran = TryCast(alotTran, PL.MESSDK.ILotTransaction_PL)
            If iLotTran IsNot Nothing Then
                'iLotTran.xxxx(aDataRow)
                MsgBox("In Developpement")
            End If
            'MessageWindow.ShowMessage("All Components have been Released", MsgBoxStyle.Information)
        Catch ex As Exception
            MessageWindow.ShowMessage(ex, MsgBoxStyle.Information)
        End Try
    End Sub

    Public Overridable Sub CreateLotDocument(ByVal sender As System.Object, ByVal e As EventArgs)

        'Me.Cursor = Cursors.WaitCursor

        Dim aDataRow As DataRow = CType(CType(sender, DXMenuItem).Tag, DataRow)
        Dim ParentLotId_FK As Guid
        Dim sqlcmd As SqlClient.SqlCommand = Nothing

        If aDataRow IsNot Nothing Then
            ParentLotId_FK = Guid.Parse(aDataRow("LotId").ToString)

            Dim report As DevExpress.XtraReports.UI.XtraReport = CreateReport(ParentLotId_FK)
            report.ShowPreview()

            If report IsNot Nothing Then
                Using reportStream As IO.MemoryStream = New IO.MemoryStream()
                    Try
                        report.ExportToHtml(reportStream)
                        reportStream.Position = 0

                        Using streamReader As IO.StreamReader = New IO.StreamReader(reportStream)
                            sqlcmd = MetaQuery.GetSPCmdByMetaName("LotDocument_PL_Save")
                            sqlcmd.Parameters("@LotDocumentID").Value = Guid.NewGuid
                            sqlcmd.Parameters("@LotID_FK").Value = ParentLotId_FK
                            sqlcmd.Parameters("@LotDocumentDate").Value = Date.Today
                            sqlcmd.Parameters("@LotDocument").Value = streamReader.ReadToEnd 'reportStream.ToArray

                            sqlcmd.ExecuteNonQuery()
                        End Using
                    Catch ex As Exception
                        Throw ex
                    Finally
                        If sqlcmd IsNot Nothing Then
                            sqlcmd.Connection.Close()
                            sqlcmd.Dispose()
                        End If
                    End Try
                End Using
            Else
                MessageBox.Show(String.Format("{0}, Not Found in Report Definition. Notification abord", report.Name))
            End If
        End If

        'Me.Cursor = Cursors.Default

    End Sub

    Public Overridable Function CreateReport(ByVal ParentLotId_FK As Guid) As XtraReport
        Dim report As DevExpress.XtraReports.UI.XtraReport = SCIEF.ActiveFramework.CommonExt.RptNotification.GetReportFromDB("CalculDeMelange")

        If report IsNot Nothing Then

            Using mquery As MetaQuery = MetaQuery.GetMetaQueryByName("CalculDeMelange")
                If mquery IsNot Nothing Then
                    mquery.SQLWhere = String.Format("LT.ParentLotId_FK = '{0}'", ParentLotId_FK)
                    mquery.FillResultQuery()
                    report.DataSource = mquery.ResultQuery
                    report.CreateDocument()
                End If
            End Using
        Else
            MessageBox.Show(String.Format("{0}, Not Found in Report Definition. Notification abord", "CalculDeMelange"))
        End If

        Return report
    End Function

    ''
    ''
    '' OutStandingIssues: A faire en Helper - aleblanc 2011-10-26
    ''
    ''
    Protected formatType As DevExpress.Utils.FormatType
    ''' <summary>
    ''' use to Build a LOV for our GRID
    ''' </summary>
    ''' <param name="queryName"></param>
    ''' <param name="sqlWhere"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Protected Function LOVBuilder(ByVal queryName As String, ByVal sqlWhere As String, ByVal sqlFrom As String, ByVal filter As String) As RepositoryItemGridLookUpEdit
        Dim aEdit As RepositoryItemGridLookUpEdit
        Dim colVisible As Boolean
        Dim hideCol As Boolean
        Dim colname As String
        Dim Column As DevExpress.XtraGrid.Columns.GridColumn
        Dim view As Views.Base.ColumnView

        Using mq As MetaQuery = MetaQuery.GetMetaQueryByName(queryName)
            If Not String.IsNullOrWhiteSpace(sqlWhere) Then
                mq.SQLWhere = sqlWhere
            End If
            '

            'ICI Le DataField filter appliquer quand on a la securité
            If Not String.IsNullOrWhiteSpace(sqlFrom) Then
                mq.SQLFrom = String.Concat(mq.SQLFrom, sqlFrom)
            End If

            If Not String.IsNullOrWhiteSpace(filter) Then
                mq.SQLWhere = String.Concat(mq.SQLWhere, filter)
            End If
            '
            mq.FillResultQuery()
            aEdit = New RepositoryItemGridLookUpEdit() With {.DataSource = mq.ResultQuery, .DisplayMember = mq.ResultQuery.Columns(CommonExt.Base.AppManager.ApplicationOption.DescriptionColumnIndex).ToString, .ValueMember = mq.GUIDField, .PopupFormSize = New Size(800, 300), .NullText = "(none)"}
            aEdit.View.HorzScrollVisibility = Views.Base.ScrollVisibility.Always
            aEdit.View.OptionsView.ColumnAutoWidth = False
            aEdit.View.OptionsView.ShowAutoFilterRow = True
            aEdit.View.OptionsBehavior.ReadOnly = True
            view = aEdit.View
            view.Columns.Clear()
            For Each row As DataRow In mq.MetaColumns.Rows
                colname = row("ColumnName").ToString
                hideCol = False
                If AppManager.ApplicationOption.DescriptionColumnIndex = 2 Then
                    If colname.EndsWith("_ALT") Then
                        hideCol = True
                    End If
                Else
                    If colname.EndsWith("_PRM") Then
                        hideCol = True
                    End If
                End If
                If hideCol = False Then
                    colVisible = CBool(IIf(CInt(row("pVisible")) = 0, False, True))
                    Column = view.Columns.AddField(colname)
                    Column.VisibleIndex = CInt(row("pOrder"))
                    Column.Caption = row("pCaption").ToString
                    Column.Visible = colVisible
                    Column.Width = CInt(row("pWidth"))
                    Column.ToolTip = row("pToolTip").ToString

                    formatType = GetFormatType(row)
                    '
                    Column.DisplayFormat.FormatType = formatType
                    Column.DisplayFormat.FormatString = row("pNumberFormat").ToString
                End If
            Next
            aEdit.DataSource = mq.ResultQuery
            Return aEdit
        End Using
    End Function

    Protected Function GetFormatType(ByVal arow As DataRow) As DevExpress.Utils.FormatType
        If (Not String.IsNullOrEmpty(arow("pNumberFormatID_FK").ToString)) Then
            Select Case CType(arow("pNumberFormatID_FK"), Guid)

                Case CommonExt.Base.IdRepository.META_NUMBERFORMAT_CURRENCY, CommonExt.Base.IdRepository.META_NUMBERFORMAT_GENERALNUMBER, CommonExt.Base.IdRepository.META_NUMBERFORMAT_PERCENT, CommonExt.Base.IdRepository.META_NUMBERFORMAT_STANDARD, CommonExt.Base.IdRepository.META_NUMBERFORMAT_SCIENTIFIC
                    Return DevExpress.Utils.FormatType.Numeric

                Case CommonExt.Base.IdRepository.META_NUMBERFORMAT_GENERALDATE, CommonExt.Base.IdRepository.META_NUMBERFORMAT_LONGDATE, CommonExt.Base.IdRepository.META_NUMBERFORMAT_LONGTIME, CommonExt.Base.IdRepository.META_NUMBERFORMAT_MEDIUMDATE, CommonExt.Base.IdRepository.META_NUMBERFORMAT_MEDIUMTIME, CommonExt.Base.IdRepository.META_NUMBERFORMAT_SHORTDATE, CommonExt.Base.IdRepository.META_NUMBERFORMAT_SHORTTIME
                    Return DevExpress.Utils.FormatType.DateTime

                Case CommonExt.Base.IdRepository.META_NUMBERFORMAT_FORMATEXPR
                    Return DevExpress.Utils.FormatType.Custom

                Case Else
                    Return DevExpress.Utils.FormatType.None
            End Select
        Else
            Return DevExpress.Utils.FormatType.None
        End If
    End Function
    ''
    ''
    '' OutStandingIssues: FIN A faire en Helper - aleblanc 2011-10-26
    ''
    ''
End Class
